package sk.ogame.realm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import sk.ogame.realm.entity.SysColumn;
import sk.ogame.realm.entity.SysTable;
import sk.ogame.realm.service.ClientHttpService;
import sk.ogame.realm.service.DatabaseService;

import java.util.List;
import java.util.Scanner;

/**
 * @author 支绍昆
 * 2023/1/28
 * console
 **/
@Component
public class Command implements CommandLineRunner {

    @Autowired
    private DatabaseService databaseService;
    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void run(String... args) throws Exception {
        new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.print("cmd>");

            String[] cmd;
            while (scanner.hasNextLine()) {
                try {
                    cmd = scanner.nextLine().split(" ");
                    switch (cmd[0]) {
                        case "do": {

                            ClientHttpService bean = applicationContext.getBean(ClientHttpService.class);
                            
                        }
                        break;

                        case "desc": {
                            List<SysColumn> sysColumns = databaseService.descTable(cmd[1]);
                            for (SysColumn column : sysColumns) {
                                System.out.println(column.getName() + " " + column.getDataType());
                            }
                        }
                        break;
                        case "show": {
                            if ("tables".equals(cmd[1])) {
                                List<SysTable> userTables = databaseService.getUserTables();
                                for (SysTable t : userTables) {
                                    System.out.println(t.getName() + " " + t.getId());
                                }
                            }
                        }
                        break;
                        case "init": {
                            databaseService.createTable("CREATE TABLE star  (\n" +
                                    "  id int NOT NULL,\n" +
                                    "  galaxy int NOT NULL,\n" +
                                    "  system int NOT NULL,\n" +
                                    "  slot int NOT NULL,\n" +
                                    "  lately int NOT NULL,\n" +
                                    "  attack int,\n" +
                                    "  rank int NOT NULL,\n" +
                                    "  build int,\n" +
                                    "  resource int,\n" +
                                    "  PRIMARY KEY (id)\n" +
                                    ")");
                        }
                        break;
                        case "exit": {
                            System.exit(0);
                        }
                        break;
                        default: {
                            System.out.println("\t unknown command [" + cmd[0] + "]");
                        }
                    }
                    System.out.print("cmd>");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            scanner.close();
        }).start();
    }

}
